package net.thxjj.adminServer.controller;

import java.util.List;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.entity.*;
import net.thxjj.adminServer.service.IProductService;
import net.thxjj.adminServer.service.iCmdationService;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import net.thxjj.sitermodel.Cmdation;
import net.thxjj.sitermodel.ParamProperty;
import net.thxjj.sitermodel.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.service.IParamService;

@RestController
@RequestMapping("/api/params")
public class ParamController extends BaseController{
	
	@Autowired
	private IParamService iparamService;

	@Autowired
	private IProductService iProductService;
	
	@Autowired
	private iCmdationService icmdationService;
	
	/**
	 * 获取参数列表 
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public Object list(){

		String prod_key = this.getParameterMap().getString("prod_key");

		if(Tools.isEmpty(prod_key)){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}

		Product product =  iProductService.getProduct(prod_key);
		if(product==null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}

		List<ParamProperty> list = iparamService.list(prod_key);


		return ResponseModel.getModel(ResultEnum.SUCCESS,list);
	}


	/**
	 * 获取命令列表
	 * @return
	 */
	@RequestMapping(value="/cmdList",method=RequestMethod.GET)
	public Object cmdList(){

		String prod_key = this.getParameterMap().getString("prod_key");

		if(Tools.isEmpty(prod_key)){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}

		Product product =  iProductService.getProduct(prod_key);
		if(product==null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}

		List<Cmdation> list = icmdationService.list(prod_key);


		return ResponseModel.getModel(ResultEnum.SUCCESS,list);
	}

	/**
	 * 添加/编辑属性参数
	 * @return
	 */
	@RequestMapping(value="/paramOperation",method=RequestMethod.POST)
	@ResponseBody
	public Object add(@RequestBody JSONObject jsonObject){


		boolean flag_update = false;
		if(jsonObject.containsKey("paramId")){
			flag_update = true;
			if(!Tools.isLongNumber(jsonObject.getString("paramId"))){
				return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
			}

			long id = jsonObject.getLongValue("paramId");
			ParamProperty paramProperty = iparamService.getParamInfo(id);
			if(paramProperty==null){
				return ResponseModel.getModel(ResultEnum.PRODUCT_PARAM_NOT_EXIST_ERROR,null);
			}
		}



		String prod_key = jsonObject.getString("prodKey");


		String identifier = jsonObject.getString("identifier");

		if(!jsonObject.containsKey("type")||Tools.isEmpty(prod_key)||Tools.isEmpty(identifier)){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}


		Product product = iProductService.getProduct(prod_key);
		if(product==null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}


		ParamProperty paramProperty = iparamService.getParamInfoByProdAndParam(prod_key, identifier);
		if (paramProperty != null) {
			if(!flag_update) {
				return ResponseModel.getModel(ResultEnum.PRODUCT_PARAM_IS_ALREADY_EXIST_ERROR, null);
			}else{
				long id = jsonObject.getLongValue("paramId");
				if(paramProperty.getParamId()!=id){
					return ResponseModel.getModel(ResultEnum.PRODUCT_PARAM_IS_ALREADY_EXIST_ERROR, null);
				}
			}
		}

		long type = jsonObject.getLongValue("type");
		if(type<1||type>3){
			return ResponseModel.getModel(ResultEnum.PARAMETER_VALUE_ERROR,null);
		}

		String unit_name = jsonObject.getString("unitName");

		String unit_symbol = jsonObject.getString("unit");

		String enum_values = jsonObject.getString("enum_values");

		String name = jsonObject.getString("name");

		String canRw = jsonObject.getString("canRw");

		if(Tools.isEmpty(name)){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}


		switch ((int)type){
			case 1:
            if(Tools.isEmpty(unit_name)){
				return ResponseModel.getModel(ResultEnum.PARAM_UNIT_NAME_NOT_EXIST_ERROR,null);
			}

			if(Tools.isEmpty(unit_symbol)){
				return ResponseModel.getModel(ResultEnum.PARAM_UNIT_NOT_EXIST_ERROR,null);
			}

			if(!jsonObject.containsKey("max")){
				return ResponseModel.getModel(ResultEnum.PARAM_UNIT_MAX_NOT_EXIST_ERROR,null);
			}

			if(!jsonObject.containsKey("min")){
				return ResponseModel.getModel(ResultEnum.PARAM_UNIT_MIN_NOT_EXIST_ERROR,null);
			}
			long max = jsonObject.getLongValue("max");
			long min = jsonObject.getLongValue("min");
			if(max<=min){
				return ResponseModel.getModel(ResultEnum.MAX_MIN_ERROR,null);
			}
			break;
			case 3:

//            if(Tools.isEmpty(enum_values)){
//				return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
//			}
			break;
			case 2:
			default:
			break;
		}

		String descp = jsonObject.getString("descp");

		ParameterMap parameterMap = new ParameterMap();
		parameterMap.put("descp",descp);
		if(flag_update){
			parameterMap.put("param_id",jsonObject.getLongValue("paramId"));
		}
		parameterMap.put("prod_key",prod_key);
		parameterMap.put("identifier",identifier);
		parameterMap.put("type",type);
		parameterMap.put("name",name);
		parameterMap.put("can_rw",canRw);
		if(type==1){
			parameterMap.put("unit_name",unit_name);
			parameterMap.put("unit_symbol",unit_symbol);
			parameterMap.put("max",jsonObject.getLongValue("max"));
			parameterMap.put("min",jsonObject.getLongValue("min"));
		}else if(type==3){
			parameterMap.put("unit_name",null);
			parameterMap.put("unit_symbol",null);
			parameterMap.put("enum_values",enum_values);
			parameterMap.put("max",0);
			parameterMap.put("min",0);
		}else{
			parameterMap.put("unit_name",null);
			parameterMap.put("unit_symbol",null);
			parameterMap.put("max",0);
			parameterMap.put("min",0);
		}

		if(flag_update){
			return  iparamService.edit(parameterMap);
		}else{
			return  iparamService.addParam(parameterMap);
		}
	}


	/**
	 * 删除属性参数
	 * @return
	 */
	@RequestMapping(value="/paramDel",method=RequestMethod.POST)
	@ResponseBody
	public Object del(@RequestBody JSONObject jsonObject){

		if(!jsonObject.containsKey("paramId")){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}

		long id = jsonObject.getLongValue("paramId");

		ParamProperty paramProperty = iparamService.getParamInfo(id);

		if(paramProperty==null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_PARAM_NOT_EXIST_ERROR,null);
		}


		return iparamService.del(id);
	}

	/**
	 * cmd命令新增及修改接口
	 * @return
	 */
	@RequestMapping(value="/cmdOperation",method=RequestMethod.POST)
	@ResponseBody
	public Object cmdOperation(@RequestBody JSONObject jsonObject){

		boolean flag_update = false;
		String operation = jsonObject.getString("operation");
		if("update".equals(operation)){
			flag_update = true;
		}

		try {
			String prod_key = jsonObject.getString("prodKey");
			String identifier = jsonObject.getString("identifier");
			String name = jsonObject.getString("name");
			String content = jsonObject.getString("content");
			if(!jsonObject.containsKey("cmdId")
                    ||Tools.isEmpty(prod_key)
					||Tools.isEmpty(identifier)
					||Tools.isEmpty(name)
					||Tools.isEmpty(content)
					||!jsonObject.containsKey("type")){
				return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
			}

			long cmd_id = jsonObject.getLongValue("cmdId");
			long type = jsonObject.getLongValue("type");

			Product product =  iProductService.getProduct(prod_key);

			if(product==null){
				return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
			}

			Cmdation cmdation =icmdationService.getCmdation(cmd_id,prod_key);
			if(!flag_update){
				if(cmdation!=null){
					return ResponseModel.getModel(ResultEnum.CMD_ID_EXIST_ERROR,null);
				}
			}else{
				if(cmdation==null){
					return ResponseModel.getModel(ResultEnum.CMD_IS_NOT_EXIST_ERROR,null);
				}
			}


			if(cmd_id<=0 || cmd_id>100){
				return ResponseModel.getModel(ResultEnum.CMD_ID_VALUE_ERROR,null);
			}

			if(type!=0&&type!=1){
				return ResponseModel.getModel(ResultEnum.PARAMETER_VALUE_ERROR,null);
			}

			String descp = jsonObject.getString("descp");

			String[]ad = content.split(",");

			if(ad.length==0){
				return ResponseModel.getModel(ResultEnum.PARAM_LIST_IS_EMPTY_ERROR,null);
			}

			for (String item:ad) {
				long aad = Integer.parseInt(item);
				ParamProperty paramProperty = iparamService.getParamInfo(aad);
				if(paramProperty==null){
					return ResponseModel.getModel(ResultEnum.PRODUCT_PARAM_NOT_EXIST_ERROR,null);
				}else{
					if(!prod_key.equals(paramProperty.getProdKey())){
						return ResponseModel.getModel(ResultEnum.PRODUCT_PARAM_NOT_EXIST_ERROR,null);
					}
				}
			}


			ParameterMap parameterMap = new ParameterMap();
			parameterMap.put("descp",descp);
			parameterMap.put("cmd_id",cmd_id);
			parameterMap.put("prod_key",prod_key);
			parameterMap.put("type",type);
			parameterMap.put("name",name);
			parameterMap.put("identifier",identifier);
			parameterMap.put("content",content);
			return flag_update? icmdationService.editCmdation(parameterMap):icmdationService.addCmdation(parameterMap);
		}catch (JSONException e){
			return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR,null);
		}catch (NumberFormatException e2){
			return ResponseModel.getModel(ResultEnum.PARAM_LIST_FORMAT_ERROR,null);
		}

	}

	/**
	 * cmd命令删除
	 * @return
	 */
	@RequestMapping(value="/cmdDel",method=RequestMethod.POST)
	@ResponseBody
	public Object cmdDel(@RequestBody JSONObject jsonObject) {

		try {
			String prod_key = jsonObject.getString("prodKey");
			if(!jsonObject.containsKey("cmdId")||Tools.isEmpty("prodKey")){
				return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR, null);
			}
			long cmd_id = jsonObject.getLongValue("cmdId");
			Cmdation cmdation = icmdationService.getCmdation(cmd_id,prod_key);
			if(cmdation==null){
				return ResponseModel.getModel(ResultEnum.CMD_IS_NOT_EXIST_ERROR, null);
			}
			icmdationService.del(cmd_id,prod_key);
			return ResponseModel.getModel(ResultEnum.SUCCESS, null);
		}catch (JSONException e){
			return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR, null);
		}


	}

}
